package com.app.couponsharelife.ui.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import android.widget.TextView
import com.app.couponsharelife.R
import com.app.couponsharelife.ui.listener.OnClickListener
import kotlinx.android.synthetic.main.bot_location_dialog.*

/**
 * Created by guluwa on 2018/9/5.
 */
class BotLocationDialog(context: Context, themeResId: Int, val str: String, val listener: OnClickListener) : Dialog(context, themeResId) {

    private val list = mutableListOf("北京市", "天津市", "上海市", "重庆市", "河北省", "山西省",
            "辽宁省", "吉林省", "黑龙江省", "江苏省", "浙江省", "安徽省",
            "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省",
            "广东省", "甘肃省", "四川省", "贵州省", "海南省", "云南省",
            "陕西省", "广西壮族自治区", "西藏自治区", "宁夏回族自治区", "新疆维吾尔族自治区", "内蒙古自治区")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.bot_location_dialog)
        initView()
    }

    //页面初始化
    private fun initView() {
        initDialog()
        initWheelView()
        findViewById<TextView>(R.id.tvCancel).setOnClickListener {
            dismiss()
        }
        findViewById<TextView>(R.id.tvFinish).setOnClickListener {
            dismiss()
        }
    }

    private fun initWheelView() {
        mWheelView.mSelectPosition = if (list.contains(str)) list.indexOf(str) else 0
        mWheelView.list = list
        mWheelView.listener = object : OnClickListener {
            override fun click(arg1: Int, arg2: Any) {
                listener.click(arg2 as Int, list[arg2])
            }
        }
    }

    //Dialog初始化
    private fun initDialog() {
        window!!.setGravity(Gravity.BOTTOM)
        window!!.setWindowAnimations(R.style.picker_view_slide_anim)
        setCanceledOnTouchOutside(true)
        //设置dialog没有边距
        window!!.decorView.setPadding(0, 0, 0, 0)
        val lp = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = lp
    }
}